package com.cndym.utils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者: cndym
 * 日期: 2010-5-4
 * 时间: 14:28:57
 */
public class FileUtils {


    public static List<String> readFile(String filePath) {
        return readFile(filePath, "gbk");
    }


    public static List<String> readFile(String filePath, String encode) {
        List<String> list = new ArrayList<String>();
        File file = new File(filePath);
        if (file.exists() == false) {
            return new ArrayList<String>();
        }
        BufferedReader reader = null;
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file), encode);
            reader = new BufferedReader(inputStreamReader);
            String temp = null;
            while ((temp = reader.readLine()) != null) {
                if (temp.trim().equals("")) {
                    continue;
                }
                list.add(temp);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return list;
    }

    public static void safeReWriteFile(String content, String filePath) {
        writeFile(content, filePath + ".bak", true);
        rename(filePath + ".bak", filePath);
    }


    public static String writeFile(String content, String filePath,
                                   boolean override) {
        FileOutputStream fileOutputStream = null;
        File file = new File(filePath);
        String value = null;
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            fileOutputStream = new FileOutputStream(file, override);
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append(content);
            value = stringBuffer.toString();
            fileOutputStream.write(stringBuffer.toString().getBytes("GBK"));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e1) {
                }
            }
        }
        return value;
    }

    public static String writeAppendFile(String content, String filePath) {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
            out.newLine();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return content;
    }

    public static List<String> exeCurrDir(String path) {
        List<String> list = new ArrayList<String>();
        File file = new File(path);
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File sub : files) {
                list.add(sub.getAbsolutePath());
            }
        }
        return list;
    }


    public static void creatDir(String path) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdir();
        }
    }

    public static void delete(String path) {
        File file = new File(path);
        if (file.exists()) {
            file.delete();
        }
    }

    public static void exeDeleteDirExp(String path) {
        File file = new File(path);
        File[] files = file.listFiles();
        for (File f : files) {
            if (f.isFile()) {
                f.delete();
            }
            if (f.isDirectory()) {
                File[] sub = f.listFiles();
                if (sub.length == 0) {
                    f.delete();
                } else {
                    exeDeleteDirExp(f.getAbsolutePath());
                }
            }
        }
        files = file.listFiles();
        if (files.length > 0) {
            exeDeleteDirExp(path);
        }

    }

    public static void exeCreatDirExp(String filePath) {
        File file = new File(filePath);
        if (file.exists() == false) {
            file.mkdirs();
        }
    }


    public static void rename(String name, String newname) {
        delete(newname);
        File file = new File(name);
        File newFile = new File(newname);
        boolean success = file.renameTo(newFile);
        if (!success) {
            System.out.println("重命名失败");
        }
    }

    public static void exeFindFiles(List<String> list, String path) {
        if (list == null) {
            list = new ArrayList<String>();
        }
        File file = new File(path);
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f : files) {
                if (f.isFile()) {
                    list.add(f.getAbsolutePath());
                } else if (f.isDirectory()) {
                    exeFindFiles(list, f.getAbsolutePath());
                }
            }
        }
    }
}
